<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.name" placeholder="姓名"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="gettypes">查询</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="types" @row-dblclick="openDrawer" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="sn" label="编号"  sortable>
			</el-table-column>
			<el-table-column prop="name" label="数据字典名" sortable>
			</el-table-column>
			<el-table-column label="操作" width="150">
        <template  slot="header" slot-scope="scope">
          <el-button style="margin-left: 30px" type="danger" size="small" @click="handleAdd">新增</el-button>
        </template>
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="pageSize" :current-page="page" :total="total" style="float:right;">
			</el-pagination>
		</el-col>

		<!--编辑界面-->
		<el-dialog :title="title" :visible.sync="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
        <el-form-item label="编号" prop="sn">
          <el-input v-model="editForm.sn" auto-complete="off"></el-input>
        </el-form-item>
				<el-form-item label="字典名称" prop="name">
					<el-input v-model="editForm.name" auto-complete="off"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

    <!-- 抽屉 -->
    <el-drawer
        size="50%" title="字典明细" :visible.sync="drawer" :direction="direction" :before-close="handleClose">
      <!--列表-->
      <el-table :data="details" @row-dblclick="openDrawer" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
        <el-table-column type="selection" width="55">
        </el-table-column>
        <el-table-column type="index" width="60">
        </el-table-column>
        <el-table-column prop="name" label="明细名字" sortable>
        </el-table-column>
        <el-table-column l


                         abel="操作" width="150">
          <template  slot="header" slot-scope="scope">
            <el-button style="margin-left: 30px" type="danger" size="small" @click="drawerhandleAdd">新增</el-button>
          </template>
          <template scope="scope">
            <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--工具条-->
      <el-col :span="24" class="toolbar">
        <el-pagination layout="prev, pager, next" @current-change="drawerhandleCurrentChange" :page-size="dpageSize" :current-page="dpage" :total="dtotal" style="float:right;">
        </el-pagination>
      </el-col>
    </el-drawer>
    <!--抽屜的编辑界面-->
    <el-dialog :title="title" :visible.sync="drawerEditFormVisible" :close-on-click-modal="false">
      <el-form :model="drawerEditForm" label-width="80px" :rules="drawerEditFormRules" ref="drawerEditForm">
        <el-form-item label="字典名称" prop="name">
          <el-input v-model="drawerEditForm.name" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="drawerEditFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="drawerEditSubmit" >提交</el-button>
      </div>
    </el-dialog>
	</section>
</template>

<script>
	import util from '../../common/js/util'
	//import NProgress from 'nprogress'
	import { getUserListPage, removeUser, batchRemoveUser, editUser, addUser } from '../../api/api';

	export default {
		data() {
			return {
				filters: {
					name: ''
				},
        drawer: false,
        direction: 'rtl',
        title:"",
				types: [],
        details: [],
				total: 0,
				page: 1,
        pageSize:2,
        dtotal: 0,
        dpage: 1,
        dpageSize:2,
        types_id:"",
				listLoading: false,
				sels: [],//列表选中列
				editFormVisible: false,//编辑界面是否显示
        drawerEditFormVisible:false,
				editLoading: false,
				editFormRules: {
					name: [
						{ required: true, message: '请输入字典名称', trigger: 'blur' }
					],
          sn: [
            { required: true, message: '请输入编号', trigger: 'blur' }
          ]
				},
        drawerEditFormRules: {
          name: [
            { required: true, message: '请输入字典明细名称', trigger: 'blur' }
          ]
        },
				//编辑界面数据
				editForm: {
					id: 0,
					name: '',
          sn:""
				},
        drawerEditForm:{
          id: 0,
          name: '',
        }
			}
		},
		methods: {
		  getDetails(){
        let para = {
          types_id : this.types_id,
          currentPage: this.dpage,
          pageSize:this.dpageSize,
        };
        this.$http.post("/detail/detailpagelist",para).then(res=>{
          this.dtotal = res.data.total;
          this.details = res.data.data;
        })
      },
      openDrawer(row){
        this.types_id = row.id;
        this.getDetails()
        this.drawer = true
      },
      handleClose(done) {
          done();
      },
			//性别显示转换
			formatSex: function (row, column) {
				return row.sex == 1 ? '男' : row.sex == 0 ? '女' : '未知';
			},
			handleCurrentChange(val) {
				this.page = val;
				this.gettypes();
			},
      drawerhandleCurrentChange(val){
        this.dpage = val;
        this.getDetails();
      },
			//获取字典类型列表
			gettypes() {
				let para = {
          currentPage: this.page,
          pageSize:this.pageSize,
				};
				//开启忙等
				this.listLoading = true;
				//NProgress.start();
				// getUserListPage(para).then((res) => {
				// 	this.total = res.data.total;
				// 	this.types = res.data.types;
				// 	this.listLoading = false;
				// 	//NProgress.done();
				// });
        this.$http.post("/type/pagelist",para).then(res=>{
          	this.total = res.data.total;
          	this.types = res.data.data;
          	//关闭忙等
          	this.listLoading = false;
        })


			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
          this.$http.delete("/type/"+row.id).then(res=>{
            if(res.data.success){
              this.$message({
                message: res.data.msg,
                type: 'success'
              });
            }else {
              this.$message({
                message: res.data.msg,
                type: 'error'
              });
            }
            this.listLoading = false;
            this.page = 1 //跳转回首页
            this.gettypes();

          })
				}).catch(() => {

				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
        if(this.$refs['editForm']){
          this.$refs['editForm'].resetFields();//重置表单验证
        }
				this.editFormVisible = true;
				this.title = "编辑"
				this.editForm = Object.assign({}, row);
			},
			//显示新增界面
      drawerhandleAdd: function () {
        if(this.$refs['drawerEditForm']){
          this.$refs['drawerEditForm'].resetFields();//重置表单验证
        }
				this.drawerEditFormVisible = true;
        this.title = "新增"
				this.drawerEditForm = {
					name: ''
				};
			},
      //显示新增界面
      handleAdd: function () {
        if(this.$refs['editForm']){
          this.$refs['editForm'].resetFields();//重置表单验证
        }
        this.editFormVisible = true;
        this.title = "新增"
        this.editForm = {
          sn: '',
          name: ''
        };
      },
			//编辑
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '温馨提示', {}).then(() => {
							this.editLoading = true;
							//NProgress.start();
							let para = Object.assign({}, this.editForm);//克隆表单
              this.$http.post("/type/save",para).then(res=>{
                this.editLoading = false;
                if(res.data.success){
                  this.$message({
                    		message: res.data.msg,
                    		type: 'success'
                    	});
                }else {
                  this.$message({
                    message: res.data.msg,
                    type: 'error'
                  });
                }
                  this.$refs['editForm'].resetFields();
                  this.editFormVisible = false;
                  this.gettypes();//重写加载列表
              })
						});
					}
				});
			},
      drawerEditSubmit(){
        this.$refs.drawerEditForm.validate((valid) => {
          if (valid) {
            this.$confirm('确认提交吗？', '温馨提示', {}).then(() => {
              //NProgress.start();
              let para = Object.assign({}, this.drawerEditForm);//克隆表单
              para.types_id = this.types_id
              this.$http.post("/detail/save",para).then(res=>{
                if(res.data.success){
                  this.$message({
                    message: res.data.msg,
                    type: 'success'
                  });
                }else {
                  this.$message({
                    message: res.data.msg,
                    type: 'error'
                  });
                }
                this.$refs['drawerEditForm'].resetFields();
                this.drawerEditFormVisible = false;
                this.getDetails()//重新加载页面
              })
            });
          }
        });
      },
			//新增
			addSubmit: function () {
				this.$refs.addForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.addLoading = true;
							//NProgress.start();
							let para = Object.assign({}, this.addForm);
							para.birth = (!para.birth || para.birth == '') ? '' : util.formatDate.format(new Date(para.birth), 'yyyy-MM-dd');
							addUser(para).then((res) => {
								this.addLoading = false;
								//NProgress.done();
								this.$message({
									message: '提交成功',
									type: 'success'
								});
								this.$refs['addForm'].resetFields();
								this.addFormVisible = false;
								this.gettypes();
							});
						});
					}
				});
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id).toString();
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					let para = { ids: ids };
					batchRemoveUser(para).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.gettypes();
					});
				}).catch(() => {

				});
			}
		},
		mounted() {
			this.gettypes();
		}
	}

</script>

<style scoped>

</style>